package global

import (
	"database/sql"
	"fmt"
	_ "github.com/lib/pq"
	"log"
	"oktools/src/conf"
	// 执行driver.go文件中的init(),向"database/sql"注册一个mysql的驱动
	_ "github.com/go-sql-driver/mysql"
)

var DB *sql.DB

func init() {
	dbConf := conf.Conf.DataBase
	var err error

	//DB, err = sql.Open("postgres", fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", dbConf.Host, dbConf.Port, dbConf.Username, dbConf.Password, dbConf.DbName))
	url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s", dbConf.Username, dbConf.Password, dbConf.Host, dbConf.Port, dbConf.DbName, "utf8")
	DB, err = sql.Open("mysql", url)

	if err != nil {
		log.Fatalln(err)
	}
}
